﻿using SnakeGame.Lesson2;
using System;
using System.Threading;

namespace SnakeGame.Lesson1
{
    /// <summary>
    /// 场景类型枚举
    /// </summary>
    internal enum E_SceneType
    {
        /// <summary>
        /// 开始场景
        /// </summary>
        Start,

        /// <summary>
        /// 难度选择
        /// </summary>
        Difficulty,

        /// <summary>
        /// 游戏场景
        /// </summary>
        Game,

        /// <summary>
        /// 结束场景
        /// </summary>
        End
    }

    internal enum E_DifficultyLevel
    {
        Easy = 200, // 简单
        Medium = 100,   // 中等
        Hard = 50,     // 困难
        Crazy = 20 // 疯狂
    }

    
    internal class Game
    {
        private static E_DifficultyLevel difficultyLevel;
        // 游戏窗口的宽度和高度
        public const int width = 80;

        public const int height = 30;

        // 当前游戏场景
        public static ISceneUpdate nowScene;
        private static bool isInGame = false;
        public Game()
        {   // 初始化游戏窗口
            Console.SetWindowSize(width, height);
            Console.SetBufferSize(width, height);
            Console.CursorVisible = false; // 隐藏光标

            // 设置窗口标题
            Console.Title = "Snake Game";
            // 设置窗口背景色
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();
            // 设置窗口字体颜色
            Console.ForegroundColor = ConsoleColor.White;
            // 游戏开始为开始场景
            ChangeScene(E_SceneType.Start);
        }

        // 开始游戏
        public void Start()
        {
            // 游戏主循环 负责更新游戏场景
            while (true)
            {
                nowScene?.Update(); // 更新当前场景
                // 如果当前场景是游戏场景，则根据难度设置游戏速度 防止影响其他场景的游戏速度
                if (isInGame)
                {
                    Thread.Sleep((int)difficultyLevel);
                }
            }
        }

        // 切换场景
        public static void ChangeScene(E_SceneType type)
        {
            // 切换场景之前先清除当前场景
            Console.Clear();

            switch (type)
            {
                case E_SceneType.Start:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    isInGame = false;
                    nowScene = new StartScene();

                    break;

                case E_SceneType.Difficulty:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    isInGame = false;
                    nowScene = new DifficultySelect();
                    break;

                case E_SceneType.Game:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    isInGame = true;
                    nowScene = new GameScene();

                    break;

                case E_SceneType.End:
                    if (nowScene != null) nowScene = null; // 清除当前场景
                    isInGame = false;
                    nowScene = new EndScene();

                    break;
            }
        }

        // 获取游戏难度
        public static void SelectDifficulty(int difficulty)
        {
            // 根据难度设置游戏速度
            switch (difficulty)
            {
                case 0:
                    difficultyLevel = E_DifficultyLevel.Easy;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 1:
                    difficultyLevel = E_DifficultyLevel.Medium;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 2:
                    difficultyLevel = E_DifficultyLevel.Hard;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 3:
                    difficultyLevel = E_DifficultyLevel.Crazy;
                    ChangeScene(E_SceneType.Game);
                    break;

                case 4:

                    ChangeScene(E_SceneType.Start);
                    break;
            }
        }
    }
}